﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MinistryStandardPlatformBusiness.Helper;
using MinistryStandardPlatformBusiness.MessagePool.Entity;

namespace MinistryStandardPlatformBusiness.MessagePool.QueueSession
{
    /// <summary>
    /// 仅供web网站调用，未涉及到客户端
    /// </summary>
   public class WebSessionHander
    {
       public static List<LatestGpsInfo> getRealtimeGPSinfoBySim(List<string> sims)
        {
            List<LatestGpsInfo> ss = new List<LatestGpsInfo>();
            ss.AddRange(UserSession.LatestGpsInfoDic.Where(p => sims.Contains(p.Key)).Select(p => p.Value));
            System.Threading.Thread writeOnlineVehicles = new System.Threading.Thread(
                () => {
                    if(ss.Count > 0)
                    {
                        string onlines ="";
                        foreach (LatestGpsInfo lg in ss)
                            onlines += "|" + lg.SimId + "|" + lg.UpTime+";";
                        MinistryStandardPlatformBusiness.Helper.Log.WriteLog("WebSimRequestDetails", "Totel:" + ss.Count.ToString()+onlines.ToString());
                    }
                    });
            writeOnlineVehicles.IsBackground = true;
            writeOnlineVehicles.Start();
            return ss;
        }
       public static List<LatestGpsInfo> getRealtimeGPSinfoByUserid(string userid)
        {

            List<LatestGpsInfo> ss = new List<LatestGpsInfo>();

            if (userid.ToLower().Equals("admin"))
            {
                ss.AddRange(UserSession.LatestGpsInfoDic.Select(p => p.Value));
            }
            else
            {
                List<string> sims = new List<string>();
                sims.AddRange(UserSession.UserVechielRightSession.Where(p => p.Username.Equals(userid)).SelectMany(p => p.RightSims));
                ss.AddRange(UserSession.LatestGpsInfoDic.Where(p => sims.Contains(p.Key)).Select(p => p.Value));
         
            }
            System.Threading.Thread writeOnlineVehicles = new System.Threading.Thread(
              () =>
              {
                  if (ss.Count > 0)
                  {
                      string onlines = "";
                      foreach (LatestGpsInfo lg in ss)
                          onlines += "|" + lg.SimId + "|" + lg.UpTime + ";";
                      MinistryStandardPlatformBusiness.Helper.Log.WriteLog("WebUserIdRequestDetails", "Totel:" + ss.Count.ToString() + onlines.ToString());

                  }
              });
            writeOnlineVehicles.IsBackground = true;
            writeOnlineVehicles.Start();
            return ss;

        }

       public static void onLineUserHanderForWeb(string userid, string guid, int loginType = 2)
        {
            MinistryStandardPlatformBusiness.Helper.Log.WriteLog("OnLineHeartFromWeb",userid + "|" + guid + "|" + loginType.ToString() );
            UserSession.onLineUserHanderForEntity(userid, guid, loginType);
        }

       public static List<OnLineUser> OnlineUserListSessionForWeb()
       {
           return UserSession.OnlineUserListSession;
       }
       public static string getHistoryTrack(string userid,string sql,string strStartDate,string strEndDate)
        {
            string rs = "0";

            DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy-MM-dd HH:mm:sss";
            try
            {
                DateTime dts = Convert.ToDateTime(strStartDate, dtFormat);//"2014-06-23 13:33"
                DateTime dte = Convert.ToDateTime(strEndDate, dtFormat);//"2014-06-24 15:33"

                rs = DBHelper.GlobalDBWCFHelper.YExecuteSql(userid, dts, dte, sql);

                Log.WriteLog("getHistoryDetails", userid + "|" + sql + "|" + strStartDate + "|" + strEndDate);
            }
            catch (Exception)
            {
                Log.WriteLog("getHistoryError", userid + "|" + sql + "|" + strStartDate + "|" + strEndDate);
            }
            return rs;
        }

        #region 报表接口

       public static string getReportDataBySql(string userid, string sql)
       {
           if (userid != null && sql != null)
               return DBHelper.getReportDataBySqlHelper(userid, sql);
           else
               return "error";

       }

        public static string getReportDataByProc(string userid, string proName)
        {
            if (userid != null && proName != null)
                return DBHelper.getReportDataByProcHelper(userid, proName);
            else
                return "error";
        }

        public static string getReportDataByProcNoPara(string userid, string proName)
        {
            if (userid != null && proName != null)
                return DBHelper.getReportDataByProcNoParaHelper(userid, proName);
            else
                return "error";
        }

        public static string getReportFromGeoBase(string userid, string sql, string strStartDate, string strEndDate, string isGeo)
        {
            return DBHelper.getReportFromGeoBase(userid, sql, strStartDate, strEndDate, isGeo);
        }
        public static string getReportFromGeoBase(string userid, string strStartDate, string strEndDate, string sim)
        {
            return DBHelper.getReportFromGeoBase(userid,  strStartDate, strEndDate, sim);
        }

        public static string TransGeoPoiHander(string lngoffset, string latoffset)
        {
            DBService.DBHelperClient geoHelper = new DBService.DBHelperClient();
            string poi;
            try
            {
                poi  = geoHelper.GgpsTranslate2BaiduPoi(lngoffset, latoffset);
                //poi = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(poi);
                if (poi.Length < 1 || poi.ToLower().Contains("error"))
                {
                    poi = "";
                }
            }
            catch (Exception)
            {
                poi = "";
            }
            return poi;
        }
        public static string getCheckDbExitsOrNot(string dateTime)
        {
            return DBHelper.getCheckDbExitsOrNot(dateTime);
        }

        #endregion
        public static bool CellLocationApp(string sim)
        {
            return GpsInfoHelper.zmqCellLocationPack(sim);
        }

        public static bool gpsAppNotify(string sim,string gpsAppBody)
        {
            if (sim != "" && gpsAppBody != "")
                return GpsInfoHelper.zmqGpsAppPack(sim, gpsAppBody);
            else
                return false;
        }
    }
}
